package cn.iocoder.yudao.module.bk.controller.admin.publisher;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bk.controller.admin.publisher.vo.*;
import cn.iocoder.yudao.module.bk.dal.dataobject.publisher.PublisherDO;
import cn.iocoder.yudao.module.bk.service.publisher.PublisherService;

@Tag(name = "管理后台 - 认证出版社")
@RestController
@RequestMapping("/bk/publisher")
@Validated
public class PublisherController {

    @Resource
    private PublisherService publisherService;

    @PostMapping("/create")
    @Operation(summary = "创建认证出版社")
    @PreAuthorize("@ss.hasPermission('bk:publisher:create')")
    public CommonResult<Long> createPublisher(@Valid @RequestBody PublisherSaveReqVO createReqVO) {
        return success(publisherService.createPublisher(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新认证出版社")
    @PreAuthorize("@ss.hasPermission('bk:publisher:update')")
    public CommonResult<Boolean> updatePublisher(@Valid @RequestBody PublisherSaveReqVO updateReqVO) {
        publisherService.updatePublisher(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除认证出版社")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bk:publisher:delete')")
    public CommonResult<Boolean> deletePublisher(@RequestParam("id") Long id) {
        publisherService.deletePublisher(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得认证出版社")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bk:publisher:query')")
    public CommonResult<PublisherRespVO> getPublisher(@RequestParam("id") Long id) {
        PublisherDO publisher = publisherService.getPublisher(id);
        return success(BeanUtils.toBean(publisher, PublisherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得认证出版社分页")
    @PreAuthorize("@ss.hasPermission('bk:publisher:query')")
    public CommonResult<PageResult<PublisherRespVO>> getPublisherPage(@Valid PublisherPageReqVO pageReqVO) {
        PageResult<PublisherDO> pageResult = publisherService.getPublisherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PublisherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出认证出版社 Excel")
    @PreAuthorize("@ss.hasPermission('bk:publisher:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPublisherExcel(@Valid PublisherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PublisherDO> list = publisherService.getPublisherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "认证出版社.xls", "数据", PublisherRespVO.class,
                        BeanUtils.toBean(list, PublisherRespVO.class));
    }

}